/**
 * desc: form-panel, just support input and select now
 * author: Joe
 * date: 8/7/2018
 *
 * form format:
 *   # input
 *     [
 *       {
 *         key,
 *         title,
 *         width,
 *       }
 *     ]
 *
 *   # select
 *     [
 *       {
 *         key,
 *         title,
 *         width,
 *         option, <label, value>
 *       }
 *     ]
 *
 *   # button
 *     [
 *       {
 *         title,
 *         action,
 *       }
 *     ]
 *
 *   # action
 */

coreModule.component('nFormPanel', {
  templateUrl: 'components/form-panel/form-panel.template.html',
  restrict: 'E',
  bindings: {
    inputForm: '<',
    inputFormdate: '<',
    selectForm: '<',
    buttonForm: '<',
    action: '<',
    selfJson: '<'
  },
  controller: [
    '$timeout','extractJsonName',
    function($timeout, extractJsonName) {
      this.$onInit = function() {
        this.formData = {};
        if (this.selfJson !== undefined) {
            var arr = extractJsonName(this.selfJson);
            for (i = 0; i < arr.length; i++){
              this.formData[arr[i]] = this.selfJson[arr[i]];
            }
        }
        this.doAction = function($event, escape) {
          // $timeout(function(self) {
          //   return function() {
          //     if ($event.keyCode === 13) {
          //       console.log(self.formData);
          //     }
          //   };
          // }(this));
          if (escape || $event.keyCode === 13) {
            this.action(this.formData);
          }
        };
        if (angular.isArray(this.buttonForm)) {
          var self = this;
          angular.forEach(this.buttonForm, function(btn) {
            btn.doAction = function() {
              (btn.action || self.action || angular.noop)(self.formData);
            };
          });
        }
      };
    },
  ]
});
